package by.grsu.na.page.sample;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.na.app.MyWebApplication;
import by.grsu.na.dao.impl.ProductDao;
import by.grsu.na.entity.Product;




public class ProductsListPage extends WebPage {

	public ProductsListPage() {
		super();

		final ProductDao productDao = new ProductDao(MyWebApplication.DATA_XML_FOLDER);

		final List<Product> allProducts = productDao.getAll();

		add(new DataView<Product>("table-rows", new ListDataProvider<Product>(allProducts)) {
			@Override
			protected void populateItem(final Item<Product> item) {
				final Product product = item.getModelObject();
				item.add(new Label("id", product.getId()));
				item.add(new Label("type", product.getType()));
				item.add(new Label("cost", product.getCost()));
				
				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new ProductEditPage(product));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						
						productDao.delete(product.getId());
						setResponsePage(new ProductsListPage());
						
					}
				});
			}
		});

		add(new Link("createLink") {
			
			@Override
			public void onClick() {
				
				setResponsePage(new ProductEditPage(new Product()));
			}
		});
	}
}